package com.minxi.jsp.controller;

import com.minxi.jsp.dao.UserDaoImpl;
import com.minxi.jsp.javabean.Userinfo;
import com.minxi.jsp.service.Userservice;
import com.minxi.jsp.service.UserserviceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet(name = "AddServlet", urlPatterns = "/addd")
public class AddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String userName = req.getParameter("userName");
        String userPwd = req.getParameter("userPwd");
        String userPhone = req.getParameter("userPhone");
        String sex = req.getParameter("sex");

        Userinfo userInfo = new Userinfo();
        userInfo.setUserName(userName);
        userInfo.setUserPwd(userPwd);
        userInfo.setUserPhone(userPhone);
        userInfo.setUserSex(null==userInfo.getUsersex()?1:Integer.parseInt(sex));

        Userservice userService = new UserserviceImpl();
        Boolean add = userService.add(userInfo);
        if (add){
            resp.sendRedirect(req.getContextPath()+"/list");
        }else {
            req.getRequestDispatcher("jsp/add.jsp");
        }
    }

}


